/*
 * @(#)CompanyAction.java 创建于 2013年3月14日 
 * 
 * Copyright (c) 2013-2014 by Drow.  
 *
 */
package com.hangxin.basic.web;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hangxin.basic.model.Company;
import com.hangxin.basic.service.CompanyService;
import com.hangxin.commons.util.PageModel;
import com.hangxin.commons.web.BasicAction;
import com.hangxin.system.model.Syslog;
import com.hangxin.system.util.LogContent;

@Controller
public class CompanyAction extends BasicAction {
	static final Logger logger = LoggerFactory.getLogger(CompanyAction.class);

	@Resource
	private CompanyService companyServiceImpl;
	
	@RequestMapping(value = "companyActionlist")
	public String systemActioninit(HttpServletRequest request, Integer pageno,
			Integer pages, Model model) {
		pageno = (pageno == null) ? 1 : pageno;
		pages = (pages == null) ? 1 : pages;
		PageModel p = new PageModel(pageno, pages, 10);
		PageModel<Map<String, Object>> pageModel = companyServiceImpl.companylist(p);
		model.addAttribute("pageModel", pageModel);
		return "company/companylist";
	}

	@RequestMapping(value = "companyActionaddInit")
	public String companyActionaddInit(HttpServletRequest request, Model model) {
		return "company/companyadd";
	}

	@RequestMapping(value = "companyActionadd")
	public String companyActionadd(HttpServletRequest request,
			HttpServletResponse response,Company company, Model model) {
		try {
			opercompany(request,company,model);
		} catch (Exception e) {
			model.addAttribute("flag", e.getMessage());
			e.printStackTrace();
			return "company/companyadd";
		}
		return "company/companylist";
	}

	@RequestMapping(value = "companyActionupdateInit")
	public String companyActionupdateInit(HttpServletRequest request, Long id,
			Model model) {
		model.addAttribute("company", companyServiceImpl.companyFindById(id));
		return "company/companyupdate";
	}

	@RequestMapping(value = "companyActionupdate")
	public String companyActionupdate(HttpServletRequest request,
			HttpServletResponse response, Company company, Model model) {
		try {
			opercompany(request,company,model);
		} catch (Exception e) {
			model.addAttribute("flag", e.getMessage());
			e.printStackTrace();
			return "company/companyupdate";
		}
		return "company/companylist";
	}
	
	@RequestMapping(value = "companyActioninfo")
	public String companyActioninfo(HttpServletRequest request, Long id,
			Model model) {
		model.addAttribute("company", companyServiceImpl.companyFindById(id));
		return "company/companyinfo";
	}
	
	private void validate(Company company) {
		if ("".equals(company.getCompanyname())||company.getCompanyname()==null) {
			throw new RuntimeException("名称不能为空");
		}
		if ("".equals(company.getTaxno())||company.getTaxno()==null) {
			throw new RuntimeException("税号不能为空");
		}
		if ("".equals(company.getCommlimit())||company.getCommlimit()==null) {
			throw new RuntimeException("普票限额不能为空");
		}
		if ("".equals(company.getSpeclimit())||company.getSpeclimit()==null) {
			throw new RuntimeException("专票限额不能为空");
		}
		if ("".equals(company.getMachineno())||company.getMachineno()==null) {
			throw new RuntimeException("开票机号不能为空");
		}
	}

	private String getContent(String realname, Company company) {
		StringBuffer content = new StringBuffer();
		content.append("用户：");
		content.append(realname);
		content.append("添加客户税号为:" + company.getTaxno() + ",名称为:"
				+ company.getCompanyname());
		content.append(",开票机号为:" + company.getMachineno());
		content.append(",专票限额为:" + company.getSpeclimit());
		content.append(",普票限额:" + company.getCommlimit());
		return content.toString();
	}
	
	private void opercompany(HttpServletRequest request,Company company,Model model)
	{
		model.addAttribute("company", company);
		validate(company);
		String realname = request.getSession().getAttribute("realname")
				.toString();
		Syslog syslog = new Syslog(Long.parseLong(request.getSession()
				.getAttribute("lgid").toString()), realname, "",
				LogContent.LOG_SAVE, LogContent.MK_USER, getContent(
						realname, company), Long.parseLong(request
						.getSession().getAttribute("lgid").toString()));
		syslog.setAddtime(new Timestamp(new Date().getTime()));
		companyServiceImpl.save(company, syslog);
		model.addAttribute("flag", "保存成功");
		PageModel p = new PageModel(1, 1, 10);
		PageModel<Map<String, Object>> pageModel = companyServiceImpl.companylist(p);
		model.addAttribute("pageModel", pageModel);
	}
}
